#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    cell_index_label_ = new QLabel {"当前单元格坐标:", this};
    cell_index_label_->setMinimumWidth(250);
    cell_type_label_ = new QLabel {"当前单元格类型:", this};
    cell_type_label_->setMinimumWidth(200);
    student_id_label_ = new QLabel {"学生ID:", this};
    student_id_label_->setMinimumWidth(200);
    ui->statusbar->addWidget(cell_index_label_);
    ui->statusbar->addWidget(cell_type_label_);
    ui->statusbar->addWidget(student_id_label_);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_set_header_btn_clicked()
{
    QTableWidgetItem *header_item;
    QStringList header_string_list {"姓 名", "性别", "出生日期", "民族", "分数", "是否党员"};
    ui->table_widget->setColumnCount(header_string_list.count());
    for (int i = 0; i < header_string_list.count(); ++i) {
        header_item = new QTableWidgetItem {header_string_list.at(i)};
        QFont font = ui->table_widget->font();
        font.setBold(true);
        font.setPointSize(12);
        header_item->setFont(font);
        header_item->setForeground(Qt::red);
        ui->table_widget->setHorizontalHeaderItem(i, header_item);
    }
}


void MainWindow::on_set_row_btn_clicked()
{
    ui->table_widget->setRowCount(ui->row_count_spin_box->value());
}


void MainWindow::on_set_row_base_color_btn_clicked(bool checked)
{
    ui->table_widget->setAlternatingRowColors(checked);
}


void MainWindow::on_init_data_btn_clicked()
{
    QTableWidget *table = ui->table_widget;
    table->clearContents();
    for (int i = 0; i < table->rowCount(); ++i) {
        CreateOneItemRow(i, "学生" + QString::number(i),
                         i%2 == 0 ? "男" : "女",
                         QDate::currentDate(),
                         "汉族", 88, i%2 == 1);
    }
}

void MainWindow::CreateOneItemRow(int row_number, const QString &name, const QString &sex,
                                  const QDate &birthday, const QString &nation, int score, bool isPM)
{
    SetItem2TableWidget(row_number, name, CellType::kName, FieldColumnNumber::kName );
    SetItem2TableWidget(row_number, birthday.toString("yyyy-MM-dd"), CellType::kBirthday, FieldColumnNumber::kBirthday);

    static int id = 10086;
    QTableWidgetItem *sex_item = new QTableWidgetItem(sex, static_cast<int>(CellType::kSex));
    QIcon icon {sex == "男" ? "://res/nan.png" : "://res/nv.png"};
    sex_item->setIcon(icon);
    sex_item->setTextAlignment(Qt::AlignCenter);
    sex_item->setData(Qt::UserRole, id++);
    ui->table_widget->setItem(row_number, static_cast<int>(FieldColumnNumber::kSex), sex_item);

    SetItem2TableWidget(row_number, nation, CellType::kNation, FieldColumnNumber::kNation);
    SetItem2TableWidget(row_number, QString::number(score), CellType::kScore, FieldColumnNumber::kScore);
    QTableWidgetItem *pm_item = new QTableWidgetItem("党员", static_cast<int>(CellType::kPM));
    if(isPM) {
        pm_item ->setCheckState(Qt::Checked);
    } else {
        pm_item ->setCheckState(Qt::Unchecked);
    }
    pm_item->setTextAlignment(Qt::AlignCenter);
    ui->table_widget->setItem(row_number, static_cast<int>(FieldColumnNumber::kPM), pm_item);
}

void MainWindow::SetItem2TableWidget(int row_number, const QString &text, const CellType &cell_type,
                                     const FieldColumnNumber &field_column_number)
{
    QTableWidgetItem *item = new QTableWidgetItem(text, static_cast<int>(cell_type));
    item->setTextAlignment(Qt::AlignCenter);
    ui->table_widget->setItem(row_number, static_cast<int>(field_column_number), item);
}

void MainWindow::SetItem2TableWidget(int row_number, const QString &text, const CellType &cell_type,
                                     const FieldColumnNumber &field_column_number, Qt::Alignment alignment)
{
    QTableWidgetItem *item = new QTableWidgetItem(text, static_cast<int>(cell_type));
    item->setTextAlignment(alignment);
    ui->table_widget->setItem(row_number, static_cast<int>(field_column_number), item);
}



void MainWindow::on_insert_row_btn_clicked()
{
    ui->table_widget->insertRow(ui->table_widget->currentRow());
    CreateOneItemRow(ui->table_widget->currentRow(), "新老师", "女", QDate::fromString("1990-11-08","yyyy-MM-dd"), "满族", 10086, true);
}


void MainWindow::on_add_row_btn_clicked()
{
    int old_row_count = ui->table_widget->rowCount();
    ui->table_widget->insertRow(old_row_count);
    CreateOneItemRow(old_row_count, "新老师", "女", QDate::fromString("1990-11-08","yyyy-MM-dd"), "满族", 10086, true);
}


void MainWindow::on_delete_row_btn_clicked()
{
    ui->table_widget->removeRow(ui->table_widget->currentRow());
}


void MainWindow::on_auto_set_height_btn_clicked()
{
    ui->table_widget->resizeRowsToContents();
}


void MainWindow::on_auto_set_width_btn_clicked()
{
    ui->table_widget->resizeColumnsToContents();
}


void MainWindow::on_copy_table_cotent_btn_clicked()
{
    ui->text_edit->clear();
    QTableWidgetItem *cell_item;
    QString str;
    for (int i = 0; i < ui->table_widget->rowCount(); ++i) {
        int columnCount = ui->table_widget->columnCount();
        for (int j = 0; j < columnCount - 1; ++j) {
            cell_item = ui->table_widget->item(i, j);
            str += cell_item->text() + " ";
        }
        if (ui->table_widget->item(i, columnCount - 1)->checkState()) {
            str += "党员";
        } else {
            str += "群众";
        }
        ui->text_edit->appendPlainText(str);
        str = "";
    }
}

void MainWindow::on_enable_edit_table_btn_clicked(bool checked)
{
    if (checked) {
        ui->table_widget->setEditTriggers(QAbstractItemView::DoubleClicked |
                                          QAbstractItemView::SelectedClicked);
    } else {
        ui->table_widget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}


void MainWindow::on_show_row_header_btn_clicked(bool checked)
{
    ui->table_widget->horizontalHeader()->setVisible(checked);
}


void MainWindow::on_show_col_header_btn_clicked(bool checked)
{
    ui->table_widget->verticalHeader()->setVisible(checked);
}


void MainWindow::on_select_row_radio_btn_clicked(bool checked)
{
    ui->table_widget->setSelectionBehavior(QAbstractItemView::SelectRows);
}


void MainWindow::on_select_cell_radio_btn_clicked(bool checked)
{
    ui->table_widget->setSelectionBehavior(QAbstractItemView::SelectItems);
}


void MainWindow::on_table_widget_currentCellChanged(int currentRow, int currentColumn,
                                                    int previousRow, int previousColumn)
{
    Q_UNUSED(previousRow);
    Q_UNUSED(previousColumn);

    QTableWidgetItem *item = ui->table_widget->item(currentRow, currentColumn);
    if (nullptr == item) return;

    cell_index_label_->setText(QString::asprintf("当前单元格坐标:%d行,%d列", currentRow, currentColumn));

    cell_type_label_->setText(QString::asprintf("当前单元格类型:%d", item->type()));

    item = ui->table_widget->item(currentRow, static_cast<int>(FieldColumnNumber::kSex));

//    student_id_label_->setText(QString::asprintf("学生ID:%s", item->text().toStdString().c_str()));
    student_id_label_->setText(QString::asprintf("学生ID:%d", item->data(Qt::UserRole).toInt()));
}

